package com.example.thirdhomework

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.LayoutInflater
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.thirdhomework.databinding.ActivityMainBinding
import java.io.Serializable

const val KEY1 = "key1"

data class Person(val name:String, val age:Int):Serializable
class MainActivity : AppCompatActivity() {

//    var seconds = 0
//    var running = false
//    var wasRunning = false

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)


//        Log.d("life cycle","onCreate")

        binding.buttonThird.setOnClickListener{
            val intent = Intent(this,ThirdActivity::class.java)
            startActivity(intent)
        }
//        if (savedInstanceState != null){
//            seconds = savedInstanceState.getInt("seconds")
//            running = savedInstanceState.getBoolean("running")
//            wasRunning = savedInstanceState.getBoolean("wasRunning")
//        }
//
//        runTimer()
//
//        button_start.setOnClickListener {
//            running = true
//        }
//
//        button_stop.setOnClickListener {
//            running = false
//        }
//
//        button_restart.setOnClickListener {
//            running = true
//            seconds = 0
//        }
//
//        ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
//
//        //写运行计时器的函数
//        fun runTimer(){
//            val handler = Handler()
//            val runnable = object: Runnable{
//                override fun run(){
//                    val hours = seconds /3600
//                    val minutes = (seconds % 3600) / 60
//                    val secs = seconds % 60
//
//                    binding.editTextText.text = String.format("%02d:%02d:%02d",hours,minutes,secs)
//                    if (running) {
//                        seconds++
//                    }
//                    handler.postDelayed(this,1000)
//                }
//            }
//            handler.post(runnable)
//        }
//        override fun onSaveInstanceState(outState: Bundle){
//            super.onSaveInstanceState(outState)
//            Log.d("life cycle","onSaveInstanceState")
//            outState.putInt("seconds",seconds)
//            outState.putBoolean("running",running)
//            outState.putBoolean("wasRunning",wasRunning)
//        }
//
//        override fun onDestroy() {
//            super.onDestroy()
//            Log.d("life cycle","onDestroy")
//        }
//        override fun onStart(){
//            super.onStart()
//            Log.d("life cycle","onStart")
//        }
//        override fun onStop(){
//            super.onStop()
//            Log.d("life cycle","onStop")
//        }
//        override fun onRestart() {
//            super.onRestart()
//            Log.d("life cycle","onRestart")
//        }
//        override fun onResume() {
//            super.onResume()
//            Log.d("life cycle","onResume")
//            if (wasRunning){
//                running = true
//            }
//        }
//        override fun onPause() {
//            super.onPause()
//            Log.d("life cycle","onPause")
//            wasRunning = running
//            running = false
//        }
//






        binding.buttonFirst.setOnClickListener{
            val intent = Intent(this,FirstActivity::class.java)
            val person = Person(binding.editTextText.text.toString(),20)
            intent.putExtra(KEY1,person)
//            intent.putExtra(KEY1,binding.editTextText.text.toString())
            startActivity(intent)
        }
        binding.buttonSecond.setOnClickListener{
            val intent = Intent(this,SecondActivity::class.java)
            startActivityForResult(intent,2)
        }

        val launcher2 = registerForActivityResult(ActivityResultContracts.TakePicturePreview()){
            binding.imageView.setImageBitmap(it)
        }

        binding.buttonPhoto.setOnClickListener{
            launcher2.launch(null)
        }

//        binding.buttonThird.setOnClickListener{
//            val intent = Intent(this,ThirdActivity::class.java)
//            startActivity(intent)
//        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 2 && resultCode == RESULT_OK){
            val str = data?.getStringExtra(KEY1)
            binding.editTextText.setText(str)
        }
    }
}